package com.rongshu.sendmessage.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.rongshu.sendmessage.R;
import com.rongshu.sendmessage.base.BaseActivity;
import com.rongshu.sendmessage.util.StringUtils;
import com.rongshu.sendmessage.util.ToastUtils;
import com.rongshu.sendmessage.util.httpUtil.HYContent;

public class UserNameActivity extends BaseActivity {

    private EditText et_user_name;
    private String source;
    private ImageButton ib_close;
    private String name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext=this;
        setContentView(R.layout.activity_user_name);
        source = getIntent().getStringExtra("source");
        name = getIntent().getStringExtra("name");
        initView();
    }

    private void initView() {
        TextView title=findViewById(R.id.title);


        LinearLayout titleLayLeft = findViewById(R.id.titleLayLeft);
        titleLayLeft.setOnClickListener(view->finish());
        et_user_name=findViewById(R.id.et_user_name);
        ib_close=findViewById(R.id.ib_close);
        if (StringUtils.isEmpty(name)){
            et_user_name.setText("");
        }else{
            et_user_name.setText(name);
            et_user_name.setSelection(name.length());
            ib_close.setVisibility(View.VISIBLE);
        }
        if ("name".equals(source)){
            title.setText("设置昵称");
            et_user_name.setHint("请输入您的昵称");
        }else{
            title.setText("设置个性签名");
            et_user_name.setHint("请输入您的个性签名");
        }
        et_user_name.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s!=null&&s.toString().length()>0){
                    ib_close.setVisibility(View.VISIBLE);
                    String teamStr=s.toString();
                    if ("name".equals(source)) {
                        if (teamStr.length() > 9) {
                            et_user_name.setText(teamStr.substring(0, 9));
                            et_user_name.setSelection(9);
                            ToastUtils.show(mContext, "昵称长度最多9位");
                        }
                    }else{
                        if (teamStr.length() > 20) {
                            et_user_name.setText(teamStr.substring(0, 20));
                            et_user_name.setSelection(230);
                            ToastUtils.show(mContext, "个性签名长度最多20位");
                        }
                    }
                }else{
                    ib_close.setVisibility(View.GONE);
                }
            }
        });
    }

    public void onClick(View view) {
        if (view.getId()==R.id.ib_close){
            et_user_name.setText("");
        }else if(R.id.btn_confirm==view.getId()){
            if ("name".equals(source)){
                if (et_user_name.length()<2){
                    ToastUtils.show(mContext,"姓名长度必须大于2位");
                    return;
                }
            }else{
                if (et_user_name.length()<2){
                    ToastUtils.show(mContext,"个性签名长度必须大于2位");
                    return;
                }
            }

            Intent intent=new Intent();
            intent.putExtra("name",et_user_name.getText().toString());
            setResult(HYContent.SUCCESS_CODE,intent);
            finish();
        }
    }
}